package com.ewanghuiju.app.presenter.resale;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.resale.ResaleShopDetailsContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanGoodsSpecsResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.bean.response.ShareResponBean;
import com.ewanghuiju.app.model.http.exception.ApiException;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class 转售商品详情
 * @time 2020/2/16
 */
public class ResaleShopDetailsPresenter extends RxPresenter<ResaleShopDetailsContract.View> implements ResaleShopDetailsContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public ResaleShopDetailsPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    @Override
    public void attachView(ResaleShopDetailsContract.View view) {
        super.attachView(view);
    }

    /**
     * 获取商品详情
     *
     * @param goodsid
     */
    @Override
    public void getShopInfo(String goodsid) {
        addSubscribe(mDataManager.getptigoodsInfo(goodsid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<PintuanShopListResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<PintuanShopListResponBean> optional) {
                        if (mView != null)
                            mView.showShopInfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 获取商品规格
     *
     * @param goodsid
     */
    @Override
    public void getGoodsspecsData(String goodsid) {
        addSubscribe(mDataManager.getPtigoodsSpecs(goodsid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<PintuanGoodsSpecsResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<PintuanGoodsSpecsResponBean>> optional) {
                        if (mView != null)
                            mView.showGoodsspecsData(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 检测团长身份
     *
     * @param goodsid
     */
    @Override
    public void ptigetIdentity(String goodsid) {
        addSubscribe(mDataManager.ptigetIdentity(goodsid)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView, true, false) {
                            @Override
                            public void onNext(Optional<BaseResponse> optional) {
                                if (mView != null)
                                    mView.showPtigetIdentitySuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                0=可以开团,1=不是团长身份,2=商品id为空,3=商品不存在,4=开团额度不足,5=余额不足,
                                if (mView != null) {
                                    if (e instanceof ApiException) {
                                        mView.showPtigetIdentityFail(((ApiException) e).getCode(), e.getMessage());
                                    } else {
                                        mView.showPtigetIdentityFail(100, "网络加载失败");
                                    }
                                }
                            }
                        })
        );

    }

    /**
     * 确认订单
     *
     * @param goodsid
     * @param specsid
     */
    @Override
    public void pticonfirmationOrder(String goodsid, String specsid, String group_id) {
        addSubscribe(mDataManager.pticonfirmationOrder(goodsid, specsid, group_id)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<AddressListResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<AddressListResponBean> optional) {
                        if (mView != null)
                            mView.showPticonfirmationOrder(optional.getIncludeNull(), specsid);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }

    /**
     * 获取分享信息
     *
     * @param goodsid
     * @param groupid
     */
    @Override
    public void getPtiptGoodsShare(String goodsid, String groupid) {
        addSubscribe(mDataManager.ptiptGoodsShare(goodsid, groupid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<ShareResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<ShareResponBean> optional) {
                        if (mView != null)
                            mView.showPtiptGoodsShare(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 检测是否可以开团
     *
     * @param groupid
     */
    @Override
    public void pticheckGroup(String groupid) {
        addSubscribe(mDataManager.pticheckGroup(groupid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.pticheckGroupSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }


}
